<template>
  <!-- 页面容器 -->
  <div class="app-container">
    <!-- 搜索表单区域 -->
    <el-form :inline="true" :model="queryParams" class="demo-form-inline">
      <!-- 产品名称搜索框 -->
      <el-form-item label="产品名称">
        <el-input v-model="queryParams.name" placeholder="产品名称" />
      </el-form-item>
      <!-- 状态选择器 -->
      <el-form-item label="状态">
        <el-select v-model="queryParams.status" placeholder="请选择">
          <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <!-- 类型选择器 -->
      <el-form-item label="类型">
        <el-select v-model="queryParams.type" placeholder="请选择">
          <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <!-- 操作按钮组 -->
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button type="primary" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 顶部功能区 - 新增按钮 -->
    <div class="top-fun">
      <el-button type="success" @click="handleAdd">新增</el-button>
    </div>

    <!-- 数据表格 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
      <!-- 序号列 -->
      <el-table-column align="center" label="序号" width="95">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <!-- 产品名称列 -->
      <el-table-column label="	产品名称	 " align="center">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <!-- 产品类型列 -->
      <el-table-column label="	产品类型  " align="center">
        <template slot-scope="scope">
          {{ scope.row.type == 'limit_number' ? '限购份数' : '限购金额' }}
        </template>
      </el-table-column>
      <!-- 产品ID列 -->
      <el-table-column label="产品id	" align="center">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <!-- 排序列 -->
      <el-table-column label="	排序  " align="center">
        <template slot-scope="scope">
          {{ scope.row.sort }}
        </template>
      </el-table-column>

      <!-- 新手产品标识列 -->
      <el-table-column label="	是否新手产品 " align="center">
        <template slot-scope="scope">
          {{ scope.row.isRookiePro }}
        </template>
      </el-table-column>
      <!-- 定期时长列 -->
      <el-table-column label="	定期时长	 " align="center">
        <template slot-scope="scope">
          {{ scope.row.lockDays }}
        </template>
      </el-table-column>

      <!-- 状态切换列 -->
      <el-table-column label="	状态  " align="center">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.statusB" @change="setStatus(scope.row)" />
        </template>
      </el-table-column>

      <!-- 操作列 -->
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" class="copy-btn" @click="handleshowInfo(scope.row)">查看</el-button>
          <el-button type="text" class="copy-btn" @click="handleEdit(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <div class="pagination">
      <el-pagination :current-page.sync="queryParams.pageNum" :page-sizes="[10, 20, 30, 40]"
        :page-size.sync="queryParams.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"
        @size-change="fetchData" @current-change="fetchData" />
    </div>

    <!-- 子组件 -->
    <Add ref="add" />
    <ShowInfo ref="ShowInfo" />
  </div>
</template>

<script>
import { productList, productUserEnable } from '@/api/table'
import Add from './add.vue'
import ShowInfo from './showInfo.vue'

export default {
  // 注册子组件
  components: {
    Add,
    ShowInfo
  },

  // 组件数据
  data() {
    return {
      list: null, // 表格数据列表
      listLoading: true, // 加载状态
      total: 0, // 数据总数
      date: [], // 日期数组
      // 状态选项配置
      statusOptions: [
        { label: '下架', value: '0' },
        { label: '上架', value: '1' }
      ],
      // 类型选项配置
      typeOptions: [
        { label: '限购份数', value: 'limit_number' },
        { label: '限购金额', value: 'limit_money' }
      ],
      // 查询参数对象
      queryParams: {
        'name': '', // 产品名称
        'pageNum': 1, // 当前页码
        'pageSize': 10, // 每页数量
        'status': '', // 状态
        'type': '' // 类型
      }
    }
  },

  // 生命周期钩子
  created() {
    this.fetchData() // 组件创建时获取数据
  },

  methods: {
    // 设置产品状态
    async setStatus(user) {
      const status = user.status === 1 ? 0 : 1
      await productUserEnable({ id: user.id, status: status })
      this.fetchData()
      this.$message({
        message: '操作成功',
        type: 'success'
      })
    },

    // 获取表格数据
    fetchData() {
      this.listLoading = true
      productList(this.queryParams).then(response => {
        // 处理状态显示
        response.data.records.map(item => {
          item.statusB = item.status === 1
        })
        this.list = response.data.records
        this.listLoading = false
        this.total = response.data.total
      })
    },

    // 提交查询
    onSubmit() {
      this.queryParams.pageNum = 1
      this.fetchData()
    },

    // 新增产品
    handleAdd() {
      this.$refs.add.show()
    },

    // 编辑产品
    handleEdit(e) {
      this.$refs.add.edit(e)
    },

    // 查看产品详情
    handleshowInfo(e) {
      this.$refs.ShowInfo.show(e)
    },

    // 重置查询条件
    reset() {
      this.value1 = ''
      this.value2 = ''
      this.queryParams = {
        'name': '',
        'pageNum': 1,
        'pageSize': 10,
        'status': '',
        'type': ''
      }
      this.fetchData()
    }
  }
}
</script>
